<?php
/**
 * Clase Utils
 * @author chalodrv@yahoo.es
 */
class Utils
{
	/**
	 * Variables
	 */

	/**
	 * Constructor
	 */
	function __construct()
	{
	}

	/**
	 * Método checkProtocol
	 * @param string $section
	 */
	function checkProtocol($section)
	{
		if (!isset($_SERVER["HTTPS"]) || strtolower($_SERVER["HTTPS"]) != "on") {
			$url = SSL_PREFIX.SERVER_URL.$_SERVER["REQUEST_URI"];
			header("Location: ".$url);
			exit;
		}
	}

	/**
	 * Método generateSEOUrl
	 * @param string $language
	 * @param string $section
	 * @param string $subsection
	 * @param int $id
	 */
	function generateSEOUrl($language=DEFAULT_LANGUAGE, $section=DEFAULT_SECTION, $subsection="", $id="")
	{
		$url = BASE_URL;
		//$url .= $language."/";

		if ($section == DEFAULT_SECTION && empty($subsection)) {
			return $url;
		}

		$url .= $section."/";
		if (!empty($subsection)) $url .= $subsection."/";
		if (!empty($id)) $url .= $id."/";
		return $url;
	}

	/**
	 * Método generateRandomCode
	 */
	function generateRandomCode()
	{
		return substr(md5(microtime()), 0, 20);
	}

	/**
	 * Método writeLog
	 * @param string $filename
	 * @param array $data
	 */
	function writeLog($filename, $data)
	{
		if (!empty($filename)) {
			$f = fopen(LOGS_DIR.$filename.".log", "a");
			fwrite($f, date("Y-m-d H:i:s")." :: ".implode(" :: ", $data)."\n");
			fclose($f);
		}
	}
	
	/**
	 * Método setLanguage
	 */
	function setLanguage()
	{
		$language = DEFAULT_LANGUAGE;
		if (!empty($_GET["language"]) && in_array($_GET["language"], explode(",", AVAILABLE_LANGUAGES))) {
			$language = $_GET["language"];
		}
		return $language;
	}

	/**
	 * Método redirect
	 * @param string $url
	 */
	function redirect($url)
	{
		// Redirigimos a la url especificada
		header("location: ".$url);
		exit;
	}
	
	/**
	 * Método toUpper
	 * @param string $string
	 */
	function toUpper($string)
	{
		$string = strtoupper($string);
		$string = strtr($string, "áéíóúñäëïöü", "ÁÉÍÓÚÑÄËÏÖÜ");
		return $string;
	}
	
	/**
	 * Método generateAlias
	 * @param string $string
	 */
	function generateAlias($string)
	{
		//$string = strtr($string, "áéíóúñäëïöüàèìòùçÁÉÍÓÚÑÄËÏÖÜÀÈÌÒÙÇ", "aeiounaeiouaeioucaeiounaeiouaeiouc");
		$string = str_replace("á", "a", $string);
		$string = str_replace("é", "e", $string);
		$string = str_replace("í", "i", $string);
		$string = str_replace("ó", "o", $string);
		$string = str_replace("ú", "u", $string);
		$string = str_replace("ñ", "n", $string);
		$string = str_replace("Á", "a", $string);
		$string = str_replace("É", "e", $string);
		$string = str_replace("Í", "i", $string);
		$string = str_replace("Ó", "o", $string);
		$string = str_replace("Ú", "u", $string);
		$string = str_replace("Ñ", "n", $string);
		$string = strtolower($string);
		$string = str_replace(" ", "-", $string);
		return $string;
	}
}
?>
